package com.cos.hospital.ui.record;

import com.cos.hospital.dao.beandao.DbElement;
import com.cos.hospital.dao.renewal.DbManager;
import com.cos.hospital.init.App;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.record.bean.BeanRecord;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 病历分类常量
 * @Author: Kosmos
 * @Date: 2018年09月22日 18:01
 * @Email: KosmoSakura@foxmail.com
 */
public class RecordCode {
    private static List<DbElement> dbElements;

    /**
     * @return 获取所有所有元素
     */
    static List<Integer> getAllTypes() {
        return Arrays.asList(Type.ChiefDescribe, Type.CurrentMedicalHistory,
                Type.PastHistory, Type.AllergyHistory, Type.MenstruationHistory,
                Type.TonguePulse, Type.Diagnosis, Type.Prescription, Type.AcupunctureRecord);
    }

    /**
     * @param isMale 是否为男性
     * @return 获取所有默认元素
     */
    static List<BeanRecord> getDefaultTypes(boolean isMale) {
        if (UTxt.isEmpty(dbElements)) {
            dbElements = DbManager.getDaoSession(App.getInstance()).getDbElementDao().loadAll();
        }
        //移除非默认元素 和男性月经
        for (int i = 0; i < dbElements.size(); i++) {
            DbElement element = dbElements.get(i);
            if (element.getFldIsDefault() == 0) {
                dbElements.remove(element);
            }
            //男性不来月经
            if (isMale && (RecordCode.Type.MenstruationHistory == element.getId())) {
                dbElements.remove(element);
            }
        }
        //排序
        for (int i = 0; i < dbElements.size() - 1; i++) {
            for (int j = 0; j < dbElements.size() - 1 - i; j++) {
                if (dbElements.get(j).getFldElementSort() > dbElements.get(j + 1).getFldElementSort()) {
                    DbElement temp = dbElements.get(j);
                    dbElements.set(j, dbElements.get(j + 1));
                    dbElements.set(j + 1, temp);
                }
            }
        }
        List<Integer> all = new ArrayList<>();
        all.add(Type.ChiefDescribe);
        all.add(Type.CurrentMedicalHistory);
        all.add(Type.PastHistory);
        all.add(Type.AllergyHistory);
        all.add(Type.MenstruationHistory);
        all.add(Type.TonguePulse);
        all.add(Type.Diagnosis);
        all.add(Type.Prescription);
        all.add(Type.AcupunctureRecord);
        //取id
        List<BeanRecord> list = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            list.add(new BeanRecord(all.get(i)));
        }
        dbElements.clear();//置空，下次重新初始化
        return list;
    }


    /**
     * @param type 模块唯一标志
     * @return 排序id
     * @apiNote 排序id：改动的时候注意值大小会影响排序
     */
    public static int getSortID(int type) {

        switch (type) {
            case Type.ChiefDescribe://主诉
                return SortId.ChiefDescribe;//主诉对应的排序id
            case Type.CurrentMedicalHistory://现病史
                return SortId.CurrentMedicalHistory;
            case Type.PastHistory://既往史
                return SortId.PastHistory;
            case Type.AllergyHistory://过敏史
                return SortId.AllergyHistory;
            case Type.MenstruationHistory://经产史
                return SortId.MenstruationHistory;
            case Type.TonguePulse://舌脉象
                return SortId.TonguePulse;
            case Type.Diagnosis://辩证诊断
                return SortId.Diagnosis;
            case Type.Prescription://治疗处方
                return SortId.Prescription;
            case Type.AcupunctureRecord://针灸记录
                return SortId.AcupunctureRecord;
            default:
                return SortId.ChiefDescribe;//默认返回主诉，如果走到这里应该是出问题了
        }
    }

    /**
     * 模块动态排序id
     */
    private interface SortId {
        int ChiefDescribe = getSort(Type.ChiefDescribe);//主诉
        int CurrentMedicalHistory = getSort(Type.CurrentMedicalHistory);//现病史
        int PastHistory = getSort(Type.PastHistory);//既往史
        int AllergyHistory = getSort(Type.AllergyHistory);//过敏史
        int MenstruationHistory = getSort(Type.MenstruationHistory);//经产史
        int TonguePulse = getSort(Type.TonguePulse);//舌脉象
        int Diagnosis = getSort(Type.Diagnosis);//辩证诊断
        int Prescription = getSort(Type.Prescription);//治疗处方
        int AcupunctureRecord = getSort(Type.AcupunctureRecord);//针灸记录
    }

    private static int getSort(int typeId) {
        if (UTxt.isEmpty(dbElements)) {
            dbElements = DbManager.getDaoSession(App.getInstance()).getDbElementDao().loadAll();
        }
        for (DbElement element : dbElements) {
            if (element.getId() == typeId) {
                return element.getFldElementSort();
            }
        }
        return 0;
    }


    /**
     * 模块唯一标志
     */
    public interface Type {
        int ChiefDescribe = 19;//主诉
        int CurrentMedicalHistory = 21;//现病史
        int PastHistory = 23;//既往史
        int AllergyHistory = 33;//过敏史
        int MenstruationHistory = 29;//经产史
        int TonguePulse = 31;//舌脉象
        int Diagnosis = 74;//辩证诊断
        int Prescription = 41;//治疗处方
        int AcupunctureRecord = 44;//针灸记录
        int FirstOrReTreament = 17;//初复诊标记

    }

    public interface FormulaId {
        int FID_LMP_1 = 1;//月经史
        int FID_LMP_2 = 2;//月经史
        int FID_GP = 3;//怀孕史
        int FID_Birth = 4;//生产史
    }
}
